package com.fline.simplesys.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Auther: csh
 * @Date: 2019/11/6 11:18
 * @Description:
 */
@Controller
public class UserController {

    @RequestMapping("/doLogin")
    @ResponseBody
    public String login(String name, String password) {
        /**
         * shiro编写认证的操作
         */
        //1.获取subject操作
        Subject subject = SecurityUtils.getSubject();
        //2.封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(name, password);
        //3.执行登录方法
        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            e.printStackTrace();
//            model.addAttribute("msg","用户名不存在！");
            return "fail";
            //登录失败
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
//            model.addAttribute("msg","密码错误!");
            return "fail";
        }
        return "success";
    }
}
